if (!String.prototype.contains) {
	String.prototype.contains = function (str) {
		return this.indexOf(str) > -1;
	};
}
if (!String.prototype.replaceAll) {
	String.prototype.replaceAll = function (searchStr, replaceStr) {
		var str = this;
		if (str.indexOf(searchStr) === -1) {
			return str;
		}
		return str.replace(searchStr, replaceStr).replaceAll(searchStr, replaceStr);
	};
}
if (!window.base64Encode) {
	window.base64Encode = function (str) {
		try {
			return btoa(
				encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) {
					return String.fromCharCode(parseInt(p1, 16));
				})
			);
		} catch (ex) {
			return btoa(str);
		}
	};
}
const nProxyInterface = {
	parseQuery: function (queryString) {
		var query = {};
		var pairs = (queryString[0] === "?" ? queryString.substr(1) : queryString).split("&");
		for (var i = 0; i < pairs.length; i++) {
			var pair = pairs[i].split("=");
			query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || "");
		}
		return query;
	},
	format: function (str) {
		const query = this.parseQuery(str);
		const p = Object.keys(query);
		var newQuery = "/proxyAPI?";
		newQuery += "uzc=" + p[0];
		for (var i = 1; i < p.length; i++) {
			const q = p[i];
			const v = query[q];
		}
		return newQuery;
	},
};
window.nProxyInterface = nProxyInterface;
const RedwoodPreprocessor = {
	preprocessorVersion: "17-10-2021",
	preprocessorABI: 2,
	DEBUGGING_ENABLED: false,
	isVideoURLAllowed: function () {
		const args = arguments[0];
		const url = args[0];
		const test = args[1];
		if (this.DEBUGGING_ENABLED) console.debug("[Redwood-preprocess::isVideoURLAllowed] url: " + url + ", test: " + test);
		if (url === null || url === undefined || url.length <= 0) return false;
		if (url.contains("ftypdash") || url.contains("google/video-file") || url.contains("\ufffd\u0077\u0065\u0062\u006d\u0042\ufffd")) return false;
		return this.isASCII(url, true);
	},
	isVideoURLAllowed2: function () {
		const args = arguments[0];
		const url = args[0];
		const test = args[1];
		const domainTest = args.length >= 3 ? args[2] : null;
		if (this.DEBUGGING_ENABLED) console.debug("[Redwood-preprocess::isVideoURLAllowed2] url: " + url + ", test: " + test + ", domainTest: " + domainTest);
		if (url === null || url === undefined || url.length <= 0) return false;
		if (url.contains("ftypdash") || url.contains("google/video-file") || url.contains("\ufffd\u0077\u0065\u0062\u006d\u0042\ufffd")) return false;
		return this.isASCII(url, true);
	},
	isASCII: function (str, extended) {
		return (extended ? /^[\x00-\xFF]*$/ : /^[\x00-\x7F]*$/).test(str);
	},
	preprocess: function () {
		const l = this;
		const proxied = window.XMLHttpRequest.prototype.open;
		window.XMLHttpRequest.prototype.open = function () {
			if (arguments.length >= 2) {
				const rURL = arguments[1];
				if (rURL.contains("//googleads.g.doubleclick.net/pagead/id")) {
					arguments[1] = "http://fuckadvertising.local/pagead/id/";
				} else if (rURL.contains("static.doubleclick.net/instream/ad_status.js")) {
					arguments[1] = "http://fuckadvertising.local/instream/ad_status.js";
				} else if (rURL.contains("www.youtube.com/api/stats")) {
					arguments[1] = "http://fuckadvertising.local/stats/submit";
				} else if (rURL.contains("www.youtube.com/ptracking")) {
					arguments[1] = "http://fuckadvertising.local/ptracking";
				} else if (rURL.contains("www.youtube.com/pagead/")) {
					arguments[1] = "http://fuckadvertising.local/pagead/blocked";
				} else if (rURL.contains(".googlevideo.com/videoplayback") && !rURL.contains("/proxyAPI")) {
					arguments[1] = "/proxyAPI?url=" + base64Encode(rURL) + "&treatAsB64";
				} else if (rURL.contains("https://manifest.googlevideo.com/api/manifest/") && rURL.contains("/captions/")) {
					arguments[1] = "/proxyAPI?url=" + base64Encode(rURL) + "&treatAsB64";
				}
				if (l.DEBUGGING_ENABLED) console.debug("[fuckAdvertising::XMLFilter] new url: " + arguments[1]);
			}
			return proxied.apply(this, [].slice.call(arguments));
		};
		const proxied_fetch = window.fetch;
		window.fetch = function () {
			if (l.DEBUGGING_ENABLED) console.debug("[fuckAdvertising::fetchFilter]", arguments);
			return proxied_fetch.apply(this, [].slice.call(arguments));
		};
		window.navigator.sendBeacon = function () {
			if (l.DEBUGGING_ENABLED) console.debug("[fuckAdvertising::Beacons]", arguments);
			return null;
		};
	},
};
window.RedwoodPreprocessor = RedwoodPreprocessor;
RedwoodPreprocessor.preprocess();
